package org.easy.excel.support.parse.source;

import lombok.Data;
import org.easy.excel.support.annotations.ExcelFormat;
import org.easy.excel.support.annotations.ExportLabel;
import org.tool.reflect.AnnotationUtil;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static cn.hutool.core.text.CharSequenceUtil.EMPTY;

/**
 * {@link ExportLabel} 注解的解析类
 *
 * @author 李坤
 * @date 2022/4/25 19:52
 */
@Data
public class ExportLabelSource implements Serializable {

    public static final String SEQUENCE = "序号";

    /**
     * 对应字段
     */
    private String field;

    /**
     * 字段对应的标题的序列号
     */
    private int sequence;

    /**
     * 该列的宽度
     */
    private int columnWidth;

    /**
     * 该字段是否进行最后的合计处理
     */
    private boolean aggregated;

    /**
     * 字段对应的标题
     */
    private List<String> titles;

    /**
     * 字段的返回值类型
     */
    private Class<?> returnType;

    /**
     * 字段上的 Format 注解
     */
    private ExcelFormatSource excelFormatSource;

    /**
     * 获取 excelFormatSource
     *
     * @return excelFormatSource
     */
    public ExcelFormatSource getExcelFormatSource() {
        if (Objects.isNull(this.excelFormatSource)) {
            return ExcelFormatSource.getDefaultExcelFormatSource();
        }
        return excelFormatSource;
    }

    /**
     * 解析 ExportLabel 注解，并生成 AnnotationExcelSource 对象
     *
     * @param exportLabel ExportLabel 注解
     *
     * @return AnnotationExcelSource 对象
     */
    public static ExportLabelSource getInstance(Field field, ExportLabel exportLabel) {
        ExportLabelSource exportLabelSource = null;

        if (Objects.nonNull(exportLabel)) {
            exportLabelSource = AnnotationUtil.parseAnnotation2InstantiatedClass(ExportLabelSource.class, exportLabel);
            exportLabelSource.setReturnType(field.getType());
            exportLabelSource.setField(field.getName());

            final ExcelFormat excelFormat = field.getAnnotation(ExcelFormat.class);
            exportLabelSource.setExcelFormatSource(AnnotationUtil.parseAnnotation2InstantiatedClass(ExcelFormatSource.class, excelFormat));
        }
        return exportLabelSource;
    }

    /**
     * 获取默认的序列对应的 AnnotationExcelSource 对象
     *
     * @return AnnotationExcelSource 对象
     */
    public static ExportLabelSource getDefaultHeaderAppend(int size) {

        ExportLabelSource sequenceAnnotationExcelSource = new ExportLabelSource();

        sequenceAnnotationExcelSource.setField(EMPTY);
        sequenceAnnotationExcelSource.setSequence(-1);
        sequenceAnnotationExcelSource.setAggregated(false);
        sequenceAnnotationExcelSource.setColumnWidth(3500);
        sequenceAnnotationExcelSource.setReturnType(void.class);

        final String[] defaultTitles;
        Arrays.fill((defaultTitles = new String[size]), SEQUENCE);
        sequenceAnnotationExcelSource.setTitles(Arrays.asList(defaultTitles));

        return sequenceAnnotationExcelSource;
    }

}
